//
//  DownLoadingArray.m
//  SinaWeather
//
//  Created by nan4 li on 12-6-19.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "DownLoadingArray.h"

@implementation DownLoadingArray
- (void)dealloc
{
    [activityDowns release];
    [super dealloc];
}

static DownLoadingArray* instance = nil;

+(DownLoadingArray*)sharedDownLoadingArray
{
	@synchronized(self)
	{
		if (!instance) {
			instance = [[DownLoadingArray alloc] init];
		}
	}
	return instance;
}

- (id)init
{
    self = [super init];
    if (self) {
        activityDowns = [[NSMutableDictionary alloc]init];
    }
    return self;
}
- (void)setDelegate:(NSString*)url delegate:(id <ImageDLoaderDelegate>)_delegate
{
    if (url==nil||_delegate==nil) {
        return;
    }
    
    NSMutableArray *pendingDelegates = [activityDowns objectForKey:url];
	if(pendingDelegates)
	{
        //已在下载的
		[pendingDelegates addObject:_delegate];
	}
	else
	{
        //加入正在下载字典
		pendingDelegates = [NSMutableArray arrayWithObject:_delegate];
		[activityDowns setObject:pendingDelegates forKey:url];
	}
}

-(BOOL)isInDownLoading:(NSString*)url
{
    if ([activityDowns objectForKey:url]==nil) {
        return FALSE;
    } 
    else {
        return TRUE;
    }
}

-(NSArray*)getDelegate:(NSString*)url
{
    NSArray *pendingDelegates = [activityDowns objectForKey:url];
    return pendingDelegates;
}

-(void)removeDelegate:(NSString*)url
{
    [activityDowns removeObjectForKey:url];
}
@end
